using System.Web.Services;
using System.Xml;
using System;

namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu Content Management System. 
	/// Web services to provide Content Management for gereal Websites using the .
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class cmsX : x_result
	{
		/// <summary>
		/// Local objects to access internal methods
		/// </summary>
		#region Private methods
		x_blog blogx;
		x_user userx;
		#endregion

		#region Protected methods
		/// <summary>Constructor</summary>
		public cmsX() : base("cmsX") {
			userx = new x_user(Application);
			blogx = new x_blog(Application);
		}
		#endregion

		#region General web service methods
		/// <summary>Logs a user on to the system</summary>
		/// <remarks>NB: this is the only method that is not validated</remarks>
		/// <param name="username">login username</param>
		/// <param name="password">login password</param>
		[WebMethod] public XmlDocument Login(string username, string password) {
			try {
				userx.Login(username, password);
				_AddOk("token", userx.Token);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

		#region Blog manage web service methods
		/// <summary>Get a blog</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="archive">the name of the archive</param>
		/// <param name="id">the blog identifier</param>
		[WebMethod] public XmlDocument Get(string token, string name, string topic, string archive, string id) {
			try {
				userx.Validate(token, "Blog.Get");
				topic = (topic == "")? "default" : topic;
				if (archive == "" || archive == "current")
					blogx.Get(name, topic, id);
				else
					blogx.Get(name, topic, archive, id);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>List blogs</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="archive">the name of the archive</param>
		[WebMethod] public XmlDocument List(string token, string name, string topic, string archive) {
			try {
				userx.Validate(token, "Blog.List");
				topic = (topic == "")? "default" : topic;
				if (archive == "" || archive == "current")
					blogx.List(name, topic);
				else
					blogx.List(name, topic, archive);
				_AddOk(blogx.Blogs.InnerXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

	}
}
